package com.phoxell;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class Serial{
	private long acc;		// device access key
	private String agn;		// agent
	private Entity ent;
	private long flg;		// flags
	private long fmw;		// firmware
	private long ofs;		// offset
	private long opt;		// options
	private boolean mdd;	// modified
	private String pml;		// serial number
	private long pdi;		// product id
	private long pds;		// product site
	private String quo;		// quotation
	private long qty;		// quantity
	private long rat;		// rate
	private long stc;		// cal
	private long stn;		// stepping
	private long stp;		// steps
	private long tmc;		// create time
	private long tme;		// time
	private long tck;		// tick
	private long trc;		// rtick charge current
	private long trv;		// rtick charge voltage
	private long vlb;		// voltage b
	private long vlc;		// voltage c
	private long vls;		// voltage s
	private String vos;		// os and version
	public Serial(String permalink){
		qty=1;
		tmc=System.currentTimeMillis();
		ent=new Entity("i24",permalink);
		if(permalink!=null){
			pml=permalink;
			try{
				Key k=KeyFactory.createKey("i24",pml);
				DatastoreService d=
						DatastoreServiceFactory.getDatastoreService();
				Entity i=d.get(k);
				try{
					acc=(Long)i.getProperty("acc");
					agn=(String)i.getProperty("agn");
					flg=(Long)i.getProperty("flg");
					fmw=(Long)i.getProperty("fmw");
					ofs=(Long)i.getProperty("ofs");
					opt=(Long)i.getProperty("opt");
					pdi=(Long)i.getProperty("pdi");
					pds=(Long)i.getProperty("pds");
					quo=(String)i.getProperty("q");
					qty=(Long)i.getProperty("qty");
					rat=(Long)i.getProperty("r");
					stc=(Long)i.getProperty("stc");
					stn=(Long)i.getProperty("stn");
					stp=(Long)i.getProperty("stp");
					tck=(Long)i.getProperty("tck");
					tmc=(Long)i.getProperty("c");
					tme=(Long)i.getProperty("t");
					trc=(Long)i.getProperty("trc");
					trv=(Long)i.getProperty("trv");
					vlb=(Long)i.getProperty("vlb");
					vlc=(Long)i.getProperty("vlc");
					vls=(Long)i.getProperty("vls");
					vos=(String)i.getProperty("vos");
					if(stc==0 || stn==0)
						throw(new Exception());
					mdd=false;
					return;
				}catch(NullPointerException e){
					Object o;
					if((o=i.getProperty("acc"))!=null)
						acc=(Long)o;
					if((o=i.getProperty("flg"))!=null)
						flg=(Long)o;
					if((o=i.getProperty("fmw"))!=null)
						fmw=(Long)o;
					if((o=i.getProperty("ofs"))!=null)
						ofs=(Long)o;
					if((o=i.getProperty("opt"))!=null)
						opt=(Long)o;
					if((o=i.getProperty("pdi"))!=null)
						pdi=(Long)o;
					if((o=i.getProperty("pds"))!=null)
						pds=(Long)o;
					if((o=i.getProperty("qty"))!=null)
						qty=(Long)o;
					if((o=i.getProperty("r"))!=null)
						rat=(Long)o;
					if((o=i.getProperty("stc"))!=null)
						stc=(Long)o;
					if((o=i.getProperty("stn"))!=null)
						stn=(Long)o;
					if((o=i.getProperty("stp"))!=null)
						stp=(Long)o;
					if((o=i.getProperty("tck"))!=null)
						tck=(Long)o;
					if((o=i.getProperty("c"))!=null)
						tmc=(Long)o;
					if((o=i.getProperty("t"))!=null)
						tme=(Long)o;
					if((o=i.getProperty("trc"))!=null)
						trc=(Long)o;
					if((o=i.getProperty("trv"))!=null)
						trv=(Long)o;
					if((o=i.getProperty("vlb"))!=null)
						vlb=(Long)o;
					if((o=i.getProperty("vlc"))!=null)
						vlc=(Long)o;
					if((o=i.getProperty("vls"))!=null)
						vls=(Long)o;
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(stc==0)
			stc=62500;
		if(stn==0)
			stn=700000;
		mdd=true;
	}

	public String getAgent(){
		return agn;
	}
	public long getCal() {
		return stc;
	}
	public short getFirmware() {
		return (short)fmw;
	}
	public long getFlags() {
		return flg;
	}
	public String getFormat(){
		return pml.substring(0,8)+"-"+pml.substring(8,12)+"-"+
				pml.substring(12,16)+"-"+pml.substring(16,20)+"-"+
				pml.substring(20,32);
	}
	public boolean getModified() {
		return mdd;
	}
	public long getStepping() {
		return stn;
	}
	public long getSteps() {
		return stp;
	}
	public long getTime() {
		return tme;
	}
	public void query(String permalink){
	}
	public void setAgent(Agent agent){
		agn="XYNC/"+agent.version;
		fmw=agent.firmware;
		vos=agent.os;
		mdd=true;
	}
	public void setFlags(long value){
		flg=value;
		mdd=true;
	}
	public void setStepping(long value) {
		stn=value;
	}
	public void setTime(long value){
		tme=value;
		mdd=true;
	}
	public void setValue(short key,short value){
		System.out.println(key+": "+((int)value&0xffff));
		switch(key){
		case 1:
			fmw=value;
			break;
		case 3:
			tck=(((tck/1000)&0xffff0000)|value)*1000;
			break;
		case 4:
			tck=(((tck/1000)&0xffff)|(value<<16))*1000;
			break;
		case 5:
			ofs=(((ofs/1000)&0xffff0000)|value)*1000;
			break;
		case 6:
			ofs=(((ofs/1000)&0xffff)|(value<<16))*1000;
			break;
		case 7:
			vlb=value;
			break;
		case 8:
			vlc=value;
			break;
		case 9:
			vls=value;
			break;
		case 13:	// 0x0d
			break;
		case 14:	// 0x0e
			break;
		case 19:	// 0x13
			stp=(stp&0xffff0000)|value;
			break;
		case 20:	// 0x14
			stp=(stp&0xffff)|(value<<16);
			break;
		}
		mdd=true;
	}
	public String toString(){
		return pml;
	}
	public int update(boolean setTime)throws Exception{
		if(mdd || setTime){
			ent.setProperty("acc",acc);
			ent.setProperty("agn",agn);
			ent.setProperty("flg",flg);
			ent.setProperty("fmw",fmw);
			ent.setProperty("ofs",ofs);
			ent.setProperty("opt",opt);
			ent.setProperty("p",pml);
			ent.setProperty("pdi",pdi);
			ent.setProperty("pds",pds);
			ent.setProperty("q",quo);
			ent.setProperty("qty",qty);
			ent.setProperty("r",rat);
			ent.setProperty("stc",stc);
			ent.setProperty("stn",stn);
			ent.setProperty("stp",stp);
			ent.setProperty("t",setTime?System.currentTimeMillis():tme);
			ent.setProperty("tck",tck);
			ent.setProperty("c",tmc);
			ent.setProperty("trc",trc);
			ent.setProperty("trv",trv);
			ent.setProperty("vlb",vlb);
			ent.setProperty("vlc",vlc);
			ent.setProperty("vls",vls);
			ent.setProperty("vos",vos);
			DatastoreService das=DatastoreServiceFactory.getDatastoreService();
			das.put(ent);
		}
		return 1;
	}
}